package bbs.net;

import com.alibaba.fastjson.JSON;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.whu.administrator.ws.MyApplication;
import com.whu.administrator.ws.R;
import com.whu.administrator.ws.VolleySingleton;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import bbs.model.Submit;
import utils.TimeUtil;

/**
 * 发帖
 * Created by nimi on 2015/11/17.
 */
public class SendTiezi {
    Upload upload;
    Download download;
    Delegate delegate;

    BBSBoard bbsBoard;
    public  SendTiezi(Upload upload){
        this.upload=upload;
    }


    public void setDelegate(Delegate delegate) {
        this.delegate = delegate;
    }

    public Delegate getDelegate() {
        return delegate;
    }

    public Download getDownload() {
        return download;
    }

    public void setDownload(Download download) {
        this.download = download;
    }

    public void get() {

        bbsBoard = new BBSBoard();
        bbsBoard.setUser_ID(upload.getSubmit().getUser_ID());
        String time = TimeUtil.timestamp2Format(System.currentTimeMillis());
        bbsBoard.setBoard_ChangeTime(time);
        bbsBoard.setBoard_IssueTime(time);
        bbsBoard.setBoard_Content(upload.getSubmit().getBoard_Content());
        bbsBoard.setBoard_Title(upload.getSubmit().getBoard_Title());
        bbsBoard.setTopic_ID(upload.getSubmit().getTopic_ID());


        String IP = MyApplication.share().getResources().getString(R.string.IP);
        String url = "http://" + IP + "/BBS/IssueBoard";
        RequestQueue requestQueue = VolleySingleton.getVolleySingleton(MyApplication.share()).getRequestQueue();
        StringRequest stringRequest =new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            public void onResponse(String response) {
                afterGet(response);
                if(delegate!=null)
                    delegate.onResponse(response,SendTiezi.this);
            }}, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                if (delegate != null)
                    delegate.onErrorResponse(error, SendTiezi.this);

            }
        })

        {
            @Override
            protected Map<String, String> getParams () {
                Map map = new HashMap();
                map.put("data",JSON.toJSONString(bbsBoard));
              /*  map.put("submitContent",upload.getSubmit().getBoard_Content());
                map.put("subjectId",upload.getSubjectId());
                map.put("userId",upload.getSubmit().getUser().getTopic_ID());
                */
                return map;
            }
            @Override
            protected Response<String> parseNetworkResponse (NetworkResponse response){
                String temp = null;
                try {
                    temp = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }

        };
        requestQueue.add(stringRequest);
    }

    private void afterGet(String response) {
        download = JSON.parseObject(response, Download.class);
    }
    // 上行
    public static class Upload {
        // 帖子内容
        Submit submit;

        public Submit getSubmit() {
            return submit;
        }

        public void setSubmit(Submit submit) {
            this.submit = submit;

        }

    }

    // 下行数据
    public static class Download {
        // 状态
        int status;

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

    }
    public static interface Delegate{
        public void onResponse(String response, SendTiezi sendTiezi);
        public void onErrorResponse(VolleyError error, SendTiezi sendTiezi);
    }

    private class BBSBoard {
        private String board_Title;
        private String board_Content;
        private String board_IssueTime;
        private String board_ChangeTime;

        private String user_ID;
        private String topic_ID;

        public String getBoard_Title() {
            return board_Title;
        }
        public void setBoard_Title(String board_Title) {
            this.board_Title = board_Title;
        }
        public String getBoard_Content() {
            return board_Content;
        }
        public void setBoard_Content(String board_Content) {
            this.board_Content = board_Content;
        }
        public String getBoard_IssueTime() {
            return board_IssueTime;
        }
        public void setBoard_IssueTime(String board_IssueTime) {
            this.board_IssueTime = board_IssueTime;
        }
        public String getBoard_ChangeTime() {
            return board_ChangeTime;
        }
        public void setBoard_ChangeTime(String board_ChangeTime) {
            this.board_ChangeTime = board_ChangeTime;
        }
        public String getUser_ID() {
            return user_ID;
        }
        public void setUser_ID(String user_ID) {
            this.user_ID = user_ID;
        }
        public String getTopic_ID() {
            return topic_ID;
        }
        public void setTopic_ID(String topic_ID) {
            this.topic_ID = topic_ID;
        }

    }
}
